package com.cmc.cloud.cmclink.doc.service.impl.multimodaltransport;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc.cloud.cmclink.doc.api.document.emums.BusinessTypeEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SiOperateTypeEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SiValidEnum;
import com.cmc.cloud.cmclink.doc.entity.BlnoDO;
import com.cmc.cloud.cmclink.doc.entity.OperateLogDO;
import com.cmc.cloud.cmclink.doc.mapper.BlnoMapper;
import com.cmc.cloud.cmclink.doc.mapper.OperateLogMapper;
import com.cmc.cloud.cmclink.doc.rpcservice.VoyScheduleService;
import com.cmc.cloud.cmclink.doc.service.multimodaltransport.TransferAgentOperationService;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.MultiOpTransferAgentPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.MultiOpTransferAgentPageRespVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.MultiOpTransferAgentSetDoVO;
import com.cmc.cloud.cmclink.svl.api.base.enums.DateKindEnum;
import com.cmc.cloud.cmclink.svl.api.base.enums.SystemEnums;
import com.cmc.cloud.cmclink.svl.api.base.req.VoyQueryReqDTO;
import com.cmc.cloud.cmclink.svl.api.base.resp.VoyQueryRespDTO;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class TransferAgentOperationServiceImpl implements TransferAgentOperationService {

    private final static String SI_OPERATE_TYPE = "SI_Operate";

    @Resource
    private OperateLogMapper operateLogMapper;
    @Resource
    private VoyScheduleService voyScheduleService;

    @Resource
    private BlnoMapper blnoMapper;

    @Override
    public PageResult<MultiOpTransferAgentPageRespVO> queryTransferAgent(MultiOpTransferAgentPageReqVO req) {
        List<VoyQueryRespDTO> list = null;
        if (!ObjectUtils.isEmpty(req.getStartTime()) && !ObjectUtils.isEmpty(req.getEndTime())){
            VoyQueryReqDTO voyQueryReqDTO = new VoyQueryReqDTO();
            voyQueryReqDTO.setDateKind(DateKindEnum.SEARCH_PERFORM.getLabel());
            voyQueryReqDTO.setPortCode(req.getTsPort());
            // 目前eta时间查询
            voyQueryReqDTO.setDateType("ETA");
            voyQueryReqDTO.setStartDate(req.getStartTime());
            voyQueryReqDTO.setEndDate(req.getEndTime());
            voyQueryReqDTO.setSystemNumber(SystemEnums.DOC.getSystemNumber());
            //查询船期数据
            list = voyScheduleService.listByCondition(voyQueryReqDTO);
        }

        IPage<MultiOpTransferAgentPageRespVO> page = Page.of(req.getPageNo(), req.getPageSize());
        IPage<MultiOpTransferAgentPageRespVO> multiOpTransferAgentPageRespVoiPage = blnoMapper.selectMultiOpTransferAgent(page, req, list);
        return new PageResult<MultiOpTransferAgentPageRespVO>(multiOpTransferAgentPageRespVoiPage.getRecords(), multiOpTransferAgentPageRespVoiPage.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean setDo(MultiOpTransferAgentSetDoVO multiOpTransferAgentSetDoVo) {

        List<BlnoDO> blnoDoList = blnoMapper.selectBatchIds(multiOpTransferAgentSetDoVo.getBlIds());
        if (!ObjectUtils.isEmpty(blnoDoList)){
            LambdaUpdateWrapper<BlnoDO> blnoDoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            blnoDoLambdaUpdateWrapper.set(BlnoDO::getDoStatus,multiOpTransferAgentSetDoVo.getDoStatus());
            blnoDoLambdaUpdateWrapper.in(BlnoDO::getId,blnoDoList.stream().map(BlnoDO::getId).collect(Collectors.toList()));
            blnoMapper.update(null,blnoDoLambdaUpdateWrapper);
        }

        saveOperateLogBatch(multiOpTransferAgentSetDoVo.getBlIds(), SI_OPERATE_TYPE, SiOperateTypeEnum.OPERATE_SI_TWELVE.getValue(),
                SiOperateTypeEnum.OPERATE_SI_TWELVE.getLabel(), "");

        return true;
    }

    private void saveOperateLogBatch(List<Long> ids, String operateType, String operateTypeSub, String operateDesc,
                                     String remark) {
        List<BlnoDO> blnoDos = blnoMapper.selectList(new LambdaQueryWrapperX<BlnoDO>().in(BlnoDO::getId, ids).eq(BlnoDO::getValid, SiValidEnum.VALID.getValue()));
        Map<Long, String> stringMap = blnoDos.stream().collect(Collectors.toMap(BlnoDO::getId, BlnoDO::getBookingNo));
        List<OperateLogDO> operateLogDos = new ArrayList<>();
        ids.forEach(n -> {
            OperateLogDO operateLog = new OperateLogDO();
            operateLog.setBusinessId(n);
            operateLog.setBusinessNo(stringMap.get(n));
            operateLog.setBusinessType(BusinessTypeEnum.SI.getValue());
            operateLog.setOperateType(operateType);
            operateLog.setOperateTypeSub(operateTypeSub);
            operateLog.setOperateDesc(operateDesc);
            operateLog.setRemark(remark);
            operateLogDos.add(operateLog);
        });
        operateLogMapper.insertBatch(operateLogDos);
    }
}
